﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.IO;

namespace Deployment.BuildManager
{
    public class Logger
    {
        public enum EntryType
        {
            Information = 0,
            Warning = 1,
            Error = 2
        }

        public static void Log(string message, EntryType entryType)
        {
            try
            {
                string logFilePath = Environment.CurrentDirectory + Path.DirectorySeparatorChar + "BuildManager_" + DateTime.Now.ToString("yyyyddMM") + ".log";
                
                using (StreamWriter w = File.AppendText(logFilePath))
                {
                    w.WriteLine("------------------------------------------");
                    w.WriteLine(DateTime.Now.ToString());
                    w.WriteLine("{0} - {1}", entryType.ToString(), message);
                    w.Flush();
                    w.Close();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static void LogException(Exception exception, EntryType entryType, int eventID, NameValueCollection additionalInfo)
        {
            string message = FormatException(exception, entryType, eventID, additionalInfo);
            Log(message, entryType);
        }

        private static string FormatException(Exception exception, EntryType entryType, int eventID, NameValueCollection additionalInfo)
        {
            string message = String.Empty;
            message += "Timestamp: " + DateTime.Now.ToString("dd/MM/yyy HH:mm:ss") + System.Environment.NewLine;
            message += "Event ID: " + eventID.ToString() + System.Environment.NewLine;
            message += "Severity: " + entryType.ToString() + System.Environment.NewLine;
            message += System.Environment.NewLine;

            message += "Summary for " + exception.Source + ":" + System.Environment.NewLine;
            message += "======================================" + System.Environment.NewLine;
            message += "--> MachineName: " + System.Environment.MachineName + System.Environment.NewLine;
            message += "--> AppDomainName: " + System.AppDomain.CurrentDomain.FriendlyName + System.Environment.NewLine;
            message += "--> WindowsIdentity:" + System.Environment.UserDomainName + "\\" + System.Environment.UserName + System.Environment.NewLine;

            // Additional Info
            if ((additionalInfo != null) && (additionalInfo.Count > 0))
            {
                foreach (string name in additionalInfo.Keys)
                {
                    message += "--> " + name + ": " + additionalInfo[name] + System.Environment.NewLine;
                }
            }
            message += System.Environment.NewLine;

            // Exception Details
            message += "Exception Information Details:" + System.Environment.NewLine;
            message += "======================================" + System.Environment.NewLine;
            message += "Source: " + exception.Source + System.Environment.NewLine;
            message += "Exception Type: " + exception.GetType() + System.Environment.NewLine;
            message += "Message: " + exception.Message + System.Environment.NewLine;
            message += System.Environment.NewLine;
            if (exception.InnerException != null)
            {
                Exception innerException = exception.InnerException;
                message += "InnerException Information Details:" + System.Environment.NewLine;
                message += "======================================" + System.Environment.NewLine;
                message += "Source: " + innerException.Source + System.Environment.NewLine;
                message += "Exception Type: " + innerException.GetType() + System.Environment.NewLine;
                message += "Message: " + innerException.Message + System.Environment.NewLine;
                message += System.Environment.NewLine;
            }
            message += "StackTrace Information Details:" + System.Environment.NewLine;
            message += "======================================" + System.Environment.NewLine;
            message += exception.StackTrace;

            return message;
        }
    }
}
